<div id='container-fluid'>
<div class='row'>
<form method="post" action="" class="hidden-print"> 
	<div class="form-group">
	  <div class="col-md-offset-3 col-md-6">        
	<select name="season" class="form-control">

<?php

include_once './db_bb_connect.php';

if (isset($_POST['season'])) {
	$setseason = $_POST['season'];

	echo "<option id=\"selected-season\" value=\"$setseason\" selected=\"selected\">$setseason is selected</option>";
} else {
	echo "<option id=\"selected-season\" value=\"\" disabled=\"disabled\" selected=\"selected\">Select a season</option>";
}
	
$sql = "SELECT DISTINCT season 
		FROM seasons
		WHERE season > '2017/2018'
		ORDER BY season";

$result = $db->query($sql);

while ($row = $result->fetch_assoc()) {
				$season = $row['season'];
				echo "<option value=\"$season\">$season</option>";
};

?>

	</select>

<?php

if (isset($_POST['season'])) {
	$setseason = $_POST['season'];


	?>

	<br>


	<select name="category" class="form-control">
		<option id="selected-category" value="" selected="selected" disabled="disabled">Select Category</option>
		<option value="general">General</option>
		<option value="game">Game</option>
		<option value="training">Training</option>

	</select>


	<?php

}

?>

	</div>
	</div>

</form>
</div>
</div>

<?php

if (isset($_POST['season']) AND isset($_POST['category'])) {
	$setseason = $_POST['season'];
	$setcategory = $_POST['category'];
	  
		  $sql = "SELECT enddate, begindate
		  FROM seasons
		  WHERE season = '$setseason'";  

		    if ($db->query($sql)) {  
		    } else {
		      echo "Error: " . $sql . "<br>" . $db->error;
		    }

		  $result = $db->query($sql);
		  $row = $result->fetch_assoc();
		  $maxdate = $row['enddate'];
		  $mindate = $row['begindate'];

		$sql = "SELECT text, ID
			    FROM attentionpointsteams
			    WHERE category = '$setcategory'
			    AND archived = 'yes'
			    AND date_create BETWEEN '$mindate' AND '$maxdate'";

		echo "<div id='container-fluid'>";
		echo "<div class='row'>";
		echo "<h4> ". ucfirst($setcategory) . "</h4>";

		?>

<table class="table table-hover table-striped table-condensed">
    <thead>
      <tr>        

      </tr>
    </thead>
    <tbody>



		<?php

		if ($db->query($sql)) {  
			$result = $db->query($sql);
			while ($row = $result->fetch_assoc()) {
				$text = nl2br($row['text']);
				$ID = $row['ID'];
				echo "
				<tr id='$ID'>
				<td> $text </td>
					<td>
						<form> 
							<input type='hidden' name='ID' value='$ID'/>
							<button type='submit' name='submit' class='btn btn-sm btn-warning unarchive'>unarchive</button>
						</form>
					</td>
				</tr>";
				}		
		} else {
      		echo "Error: " . $sql . "<br>" . $db->error;
    		} 

    	echo "
    	</tbody>
    	</table>
    	</div>
    	</div>
    	";
    		} //close if isset

	?>

<script type="text/javascript">
	
$("select").on("change", function(e) {
    postData = $(this.form).serialize();
    $.ajax({
	  type: "POST",
	  url: "./actionpoints_team_archive.php",
	  data: postData,
	  success: function(data) {
	  	$("#maincontent").html(data);
  	  }
  }); 
  e.preventDefault();

});

$(".unarchive").on("click", function(e) {
		postData = $(this.form).serialize();
		var ID = $(this.form).find('input[name=ID]').val();
		$.ajax({
 			type: "POST",
  			url: "./includes/handlers/actionpoints_team_unarchive_item_handler.php",
  			data: postData,
  			success: function(data) {
 				$("#" + ID).html("<td colspan='2'>" + data + "</td>");				
  			},
  			error: function (xhr, status, error) {
  				alert(xhr.responseText);
  			}
  		}); 
		e.preventDefault();		
});

</script>
